package com.xinya.wanandroidclient.ui.square

import androidx.lifecycle.MutableLiveData
import com.xinya.wanandroidclient.Constants
import com.xinya.wanandroidclient.base.BaseRepository
import com.xinya.wanandroidclient.bean.NaviBean
import com.xinya.wanandroidclient.bean.SystemBean
import com.xinya.wanandroidclient.http.LoadState
import com.xinya.wanandroidclient.http.RetrofitFactory
import com.xinya.wanandroidclient.http.RetrofitManager
import com.xinya.wanandroidclient.http.WanAndroidApiService
import kotlinx.coroutines.CoroutineScope

/**
 *
 * @ClassName:      SquareRepo$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/3$ 18:51$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class SquareRepo(coroutineScope: CoroutineScope) : BaseRepository(coroutineScope) {
    fun getClassList(
        type: Int,
        systemListViewModel: MutableLiveData<SystemBean>,
        navListViewModel: MutableLiveData<NaviBean>,
        refreshStateLiveData: MutableLiveData<LoadState>,
    ) {
        launch(
            block = {
                if (type == Constants.NAV_PROJECT) {
                    RetrofitManager.getApiService(WanAndroidApiService::class.java)
                        .getSystemList()
                        .result()
                } else {
                    RetrofitManager.getApiService(WanAndroidApiService::class.java)
                        .getNaviList()
                        .result()
                }
            },
            success = {

            }
        )
    }

    fun getSystemList(
        systemListViewModel: MutableLiveData<MutableList<SystemBean>>,
        refreshStateLiveData: MutableLiveData<LoadState>,
    ) {
        launch(
            block = {
                refreshStateLiveData.postValue(LoadState.Loading)
                RetrofitManager.getApiService(WanAndroidApiService::class.java)
                    .getSystemList()
                    .result()
            },
            success = {
                refreshStateLiveData.postValue(LoadState.Success)
                systemListViewModel.postValue(it)
            },
            error = {
                refreshStateLiveData.postValue(LoadState.Error)
            }
        )
    }

    fun getNavList(
        navListViewModel: MutableLiveData<MutableList<NaviBean>>,
        refreshStateLiveData: MutableLiveData<LoadState>,
    ) {
        launch(
            block = {
                RetrofitManager.getApiService(WanAndroidApiService::class.java)
                    .getNaviList()
                    .result()
            },
            success = {
                refreshStateLiveData.postValue(LoadState.Success)
                navListViewModel.postValue(it)
            },
            error = {
                refreshStateLiveData.postValue(LoadState.Error)
            }
        )
    }

}